home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15603 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  102 lines

  1. Path: noc.netcom.net!news
  2. From: Tarang Deshpande <tarang@willows.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: Newbie using text files
  5. Date: Fri, 19 Apr 1996 12:49:45 -0700
  6. Organization: NETCOM Network Operations
  7. Message-ID: <3177EE59.773D@willows.com>
  8. References: <4l6aqu$rfr@chile.it.earthlink.net>
  9. NNTP-Posting-Host: daffy.willows.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0GoldB2 (Win95; I)
  14.  
  15. Peter Bruno wrote:
  16. > I'm new to C programming so bear with me for a bit...
  17. > I do a lot of small programming projects at work, mostly to make my
  18. > life easier, and I've been using QBasic.  I would like, however, to
  19. > use a betting langauage if I could.  But, trying to input and
  20. > processes text files in C seems a lot harder than in C, what with
  21. > pointers and all.
  22. > My point, and I do have one, is it worth my time to learn C to process
  23. > ASCII text files with fixed lenth fields, for sorting etc...
  24. > and could you suggest some routines for doing this...  in basic I just
  25. > do the following:
  26. > open "foo.bar" for input as #1
  27. > open "foo.out" for output as #2
  28. > while not eof(1)
  29. >         line input #1, row
  30. >         FName = mid$(row, 1, 25)        'characters 1-25 are first name
  31. >         LName = mid$(row, 26, 50)
  32. >         test = mid$(LName, 26, 1)
  33. >         if test < "M" then print #2, row        'if person's last name begins with
  34. > character less then M print to output file, else we don't want them!
  35. > wend
  36. > close #1, #2
  37. > can someone (kind-hearted and forgiving of stupid questions) please
  38. > offer suggestions on how to do this in C?  PLEASE!
  39. > thanks,
  40. > Peter Bruno
  41. > brunop@earthlink.net
  42.  
  43. Why are you doing these things in a program anyways?  Most O/S's that I've ever
  44. worked with will allow you to such things using a combination of commands and/or
  45. standard utilities, not to mention scripting.  However if you still want to use
  46. C then:
  47.  
  48. /* Note that this program requires the file names of the input and output files
  49. ** to be on the command line.  It also assumes that there is a fixed line length
  50. ** of 50 characters followed by new line.
  51. */
  52.  
  53.  
  54. #include <stdio.h>
  55.  
  56. int main ( int argc, char **argv )
  57. {
  58.  
  59.     FILE    *Input = NULL;
  60.     FILE    *Output = NULL;
  61.     char    Name [ 51 ];
  62.  
  63.     if ( ! ( Input = fopen ( argv [ 1 ], "r" ) ) )
  64.         fprintf ( stderr, "Could not open %s\n", argv [ 1 ] );
  65.     else
  66.     if ( ! ( Output = fopen ( argv [ 2 ], "w" ) ) )
  67.         fprintf ( stderr, "Could not open %s\n", argv [ 2 ] );
  68.     else
  69.     while ( ! ( feof ( Input ) ) )
  70.     {
  71.         fread ( Name, sizeof ( Name ), sizeof ( Name ), Input );
  72.         if ( Name [ 25 ] < 'M' )
  73.             fprintf ( "%s", Name );
  74.     }
  75.  
  76.     if ( Input )
  77.         fclose ( Input );
  78.     if ( Output )
  79.         fclose ( Output );
  80.  
  81.     return ( 0 );
  82.  
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. By the way what's a "betting" language and how can processing "text files in 
  90. C seems a lot harder than in C"?   :)
  91.  
  92. Tarang
  93.